define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_sale, GoPage, $ionicSideMenuDelegate,GoDialog) {
		$scope.scope = new Object();
		$scope.filter = new Object();
		$scope.brand = new Array();
		$scope.cat = new Array();
		//页面跳转
		$scope.onshow_productmanager_publish = function() {
			GoPage.go_states("productmanager_publish");
		}
		$scope.onshow_productmanager_publish_edit = function() {
			GoPage.go_states("productmanager_sale_edit");
		}

		//4个导航
		$scope.onshow_productmanager = function(index) {
			//if(index==1) GoPage.go_states("productmanager_sale");
			if(index == 2) GoPage.go_states("productmanager_brand");
			if(index == 3) GoPage.go_states("productmanager_waitsale");
			if(index == 4) GoPage.go_states("productmanager_sort");
		}

		//打开侧边的筛选菜单
		$scope.openRightMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		}
		
		//加载数据
		$scope.order = 1;//默认
		$scope.init_data = function() {
			Productmanager_sale.goods_search(1, 10, $scope.order, function(data) {
				$scope.datas = data.goods;
			})
			Productmanager_sale.brandList(function(data) {
				$scope.brands = data.brand;
			})
		}
		$scope.init_data();
		
		//排序--价格
		$scope.orderby_price = function(){
			if($scope.order==2){
				$scope.order = 3;
			}else{
				$scope.order = 2;
			}
			Productmanager_sale.goods_search(1, 10, $scope.order, function(data) {
				$scope.datas = data.goods;
			})
		}
		//排序--销量
		$scope.orderby_count = function(){
			if($scope.order==4){
				$scope.order = 5;
			}else{
				$scope.order = 4;
			}
			Productmanager_sale.goods_search(1, 10, $scope.order, function(data) {
				$scope.datas = data.goods;
			})
		}
		//上下架
		$scope.goods_update = function(goods_id) {
			Productmanager_sale.goods_update(goods_id, false, function(data) {
				if(data.success){
					GoDialog.toast("下架成功！");
				}else{
					GoDialog.toast("下架失败！");
				}
			})
		}
		//删除商品
		$scope.goods_delete = function(goods_id) {
			Productmanager_sale.goods_delete(goods_id, function(data) {
				if(data.success){
					GoDialog.toast("删除成功！");
					var index = -1 ;
					for (var i=0;i<$scope.datas.length;i++) {
					   if($scope.datas[i].goods_id==goods_id){
					   	index = i;
					   	break;//一找到就退出循环
					   }
					}
					if(index!=-1){//说明找到对应的商品
					   		$scope.datas.splice(index,1);
					}
				}else{
					GoDialog.toast("删除失败！");
				}
			})
		}
		
		
		$scope.scope = new Object();
		$scope.scope.brand_id = "";
		
		$scope.input_data = function() {
			var i = $scope.brand.length;
			$scope.brand[i] = new Object();
			$scope.brand[i]['brand_id'] = $scope.scope.brand_id;
			var j = $scope.cat.length;
			$scope.cat[j] = new Object();
			$scope.cat[j]['cat_id'] = $scope.scope.cat_id1;
			$scope.cat[j + 1] = new Object();
			$scope.cat[i + 1]['cat_id'] = $scope.scope.cat_id2;
			$scope.cat[j + 2] = new Object();
			$scope.cat[i + 2]['cat_id'] = $scope.scope.cat_id3;
			$scope.filter.bn = '商品编码';
			$scope.filter.brand_id = $scope.brand;
			$scope.filter.cat_id = $scope.cat;
			$scope.filter.price_frome = $scope.scope.low;
			$scope.filter.price_to = $scope.scope.high;
			$scope.filter.custom_cat_id = '店铺自定义分类ID';
			console.log($scope.filter);
		}
		$scope.reset = function() {
			$scope.scope.key = '';
			$scope.scope.brand_id = '';
			$scope.scope.cat_id1 = '';
			$scope.scope.cat_id2 = '';
			$scope.scope.cat_id3 = '';
			$scope.scope.low = '';
			$scope.scope.high = '';
			$scope.filter = new Object();
			$scope.brand = new Array();
			$scope.cat = new Array();
		}
		$scope.submit = function() {
			$scope.input_data();
			Productmanager_sale.goods_search($scope.scope.key, 1, 'filter', function(data) {
				$scope.datas = data.goods;
				$scope.filter = new Object();
				$scope.brand = new Array();
				$scope.cat = new Array();
				//$scope.reset();
				//console.log($scope.filter);
			})
		}
		
	}

	controller.$inject = ['$scope', 'Productmanager_sale', 'GoPage', '$ionicSideMenuDelegate','GoDialog'];

	return app.registerController("productmanager_salectrl",controller);
});